package com.unittec.drainage.provider.auth.service;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.unittec.drainage.provider.auth.entity.SysPermission;
import com.unittec.drainage.provider.auth.mapper.SysPermissionMapper;
import com.unittec.drainage.provider.root.ServiceRoot;
import com.unittec.drainage.sdk.root.common.CommonConstant;

/**
 * @author Jason
 * @description 菜单Service
 * @date 2019/11/07
 */
@Service
public class SysPermissionService extends ServiceRoot<SysPermissionMapper, SysPermission> {

    /**
     * 添加菜单
     *
     * @param sysPermission
     */
    @Transactional(rollbackFor = Exception.class)
    public void addPermission(SysPermission sysPermission) {
        if (CommonConstant.MENU_TYPE_0.equals(sysPermission.getMenuType())) {
            sysPermission.setParentId("");
        }
        String pid = sysPermission.getParentId();
        if (StringUtils.isNotEmpty(pid)) {
            getMapper().setMenuLeaf(pid, 0);
        }
        sysPermission.setStatus(Integer.parseInt(CommonConstant.STATUS_0));
        sysPermission.setDelFlag(Integer.parseInt(CommonConstant.DELETE_NO));
        sysPermission.setLeaf(true);
        this.save(sysPermission);
    }

    /**
     * 修改菜单
     *
     * @param sysPermission
     */
    @Transactional(rollbackFor = Exception.class)
    public void editPermission(SysPermission sysPermission) {
        SysPermission p = this.getById(sysPermission.getId());
        if (CommonConstant.MENU_TYPE_0.equals(sysPermission.getMenuType())) {
            sysPermission.setParentId("");
        }
        int count = this
            .count(new QueryWrapper<SysPermission>().lambda().eq(SysPermission::getParentId, sysPermission.getId()));
        if (count == 0) {
            sysPermission.setLeaf(true);
        }
        this.updateById(sysPermission);
        String pid = sysPermission.getParentId();
        boolean flag = (StringUtils.isNotEmpty(pid) && !pid.equals(p.getParentId()))
            || StringUtils.isBlank(pid) && StringUtils.isNotEmpty(p.getParentId());
        if (flag) {
            getMapper().setMenuLeaf(pid, 0);
            int cc =
                this.count(new QueryWrapper<SysPermission>().lambda().eq(SysPermission::getParentId, p.getParentId()));
            if (cc == 0) {
                if (StringUtils.isNotEmpty(p.getParentId())) {
                    getMapper().setMenuLeaf(p.getParentId(), 1);
                }
            }
        }
    }

    /**
     * 删除菜单
     *
     * @param sysPermission
     */
    @Transactional(rollbackFor = Exception.class)
    public void deletePermission(SysPermission sysPermission) {
        String pid = sysPermission.getParentId();
        if (StringUtils.isNotEmpty(pid)) {
            int count = this.count(new QueryWrapper<SysPermission>().lambda().eq(SysPermission::getParentId, pid));
            if (count == 1) {
                getMapper().setMenuLeaf(pid, 1);
            }
        }
        getMapper().deleteById(sysPermission.getId());
        removeChildrenBy(sysPermission.getId());
    }

    /**
     * 根据父id删除其关联的子节点数据
     *
     * @return
     */
    public void removeChildrenBy(String parentId) {
        LambdaQueryWrapper<SysPermission> query = new LambdaQueryWrapper<>();
        query.eq(SysPermission::getParentId, parentId);
        List<SysPermission> permissionList = this.list(query);
        if (permissionList != null && permissionList.size() > 0) {
            String id = "";
            int num = 0;
            this.remove(query);
            for (int i = 0, len = permissionList.size(); i < len; i++) {
                id = permissionList.get(i).getId();
                num = this.count(new LambdaQueryWrapper<SysPermission>().eq(SysPermission::getParentId, id));
                if (num > 0) {
                    this.removeChildrenBy(id);
                }
            }
        }
    }

}
